/****************************************************************
 *
 * IMU Project 
 * 
 ****************************************************************/

/****************************************************************
 *
 *	Module		: "header file" myIMU.h
 *  Tool		: KeilC ARM 4.22a
 *	Chip		: STM32F407VG
 * 	History		: 04/08/2012
 *	Description	: 
				  + My library to communicate with IMU
				  + Reference to source file "myIMU.c"
 *				
 *	Author		: Dang Anh Tung			
 *	Description	: www.payitforward.edu.vn
 *				
 *
 ****************************************************************/

#ifndef MYIMU_H
#define MYIMU_H

/****************************************************************
 * 							IMPORT
 ****************************************************************/
 #include "stm32f4xx.h"
 #include "myI2C.h"
 #include "myDef.h"
 #include <math.h>


/*****************************************************************
* 							TYPEDEF
******************************************************************/

typedef enum
{
    ACC_DATA_CONVERT,
    GYR_DATA_CONVERT,
    MAG_DATA_CONVERT
}CONVERT_TYPE;
 
/*****************************************************************
* 							DEFINE
******************************************************************/

#define ACC_SENSOR_OK           (uint32_t)0x01    
#define GYR_SENSOR_OK           (uint32_t)0x02
#define MAG_SENSOR_OK           (uint32_t)0x04


/************************************** 
*           LSM330 BOARD
**************************************/
#ifdef _USE_LSM330_BOARD_

#define K_ACC_X                 (REAL)2.0           /* ACC in 4g scale, unit is mg */
#define K_ACC_Y                 (REAL)2.0
#define K_ACC_Z                 (REAL)2.0

#define K_GYR_X                 (REAL)17.5          /* GYR in 500dps scale, unit is mdps */
#define K_GYR_Y                 (REAL)17.5
#define K_GYR_Z                 (REAL)17.5

#define K_MAG_X                 (REAL)0.9174        /* MAG in 1.3Ga scale, unit is mGa*/
#define K_MAG_Y                 (REAL)0.9174
#define K_MAG_Z                 (REAL)0.9174
   
/* I2C DEVICE ADDRESS & REGISTER ADDRESS */
#define I2C_ACC_ADDR            (uint8_t)0x18
#define I2C_ACC_READ            (uint8_t)0x31
#define I2C_ACC_WRITE           (uint8_t)0x30
#define I2C_ACC_CTRL_REG1       (uint8_t)0x20
#define I2C_ACC_CTRL_REG2       (uint8_t)0x21
#define I2C_ACC_CTRL_REG3       (uint8_t)0x22
#define I2C_ACC_CTRL_REG4       (uint8_t)0x23
#define I2C_ACC_CTRL_REG5       (uint8_t)0x24
#define I2C_ACC_CTRL_REG6       (uint8_t)0x25
#define I2C_ACC_STATUS_REG      (uint8_t)0x27
#define I2C_ACC_OUT_X_L         (uint8_t)0x28
#define I2C_ACC_OUT_X_H         (uint8_t)0x29
#define I2C_ACC_OUT_Y_L         (uint8_t)0x2A
#define I2C_ACC_OUT_Y_H         (uint8_t)0x2B
#define I2C_ACC_OUT_Z_L         (uint8_t)0x2C
#define I2C_ACC_OUT_Z_H         (uint8_t)0x2D
#define I2C_ACC_FIFO_CTRL_REG   (uint8_t)0x2E
#define I2C_ACC_FIFO_SRC_REG    (uint8_t)0x2F

#define I2C_GYR_ADDR            (uint8_t)0x68
#define I2C_GYR_READ            (uint8_t)0xD1
#define I2C_GYR_WRITE           (uint8_t)0xD0
#define I2C_GYR_CTRL_REG1       (uint8_t)0x20
#define I2C_GYR_CTRL_REG2       (uint8_t)0x21
#define I2C_GYR_CTRL_REG3       (uint8_t)0x22
#define I2C_GYR_CTRL_REG4       (uint8_t)0x23
#define I2C_GYR_CTRL_REG5       (uint8_t)0x24
#define I2C_GYR_OUT_TEMP        (uint8_t)0x25
#define I2C_GYR_STATUS_REG      (uint8_t)0x27
#define I2C_GYR_OUT_X_L         (uint8_t)0x28
#define I2C_GYR_OUT_X_H         (uint8_t)0x29
#define I2C_GYR_OUT_Y_L         (uint8_t)0x2A
#define I2C_GYR_OUT_Y_H         (uint8_t)0x2B
#define I2C_GYR_OUT_Z_L         (uint8_t)0x2C
#define I2C_GYR_OUT_Z_H         (uint8_t)0x2D
#define I2C_GYR_FIFO_CTRL_REG   (uint8_t)0x2E
#define I2C_GYR_FIFO_SRC_REG    (uint8_t)0x2F

#define I2C_MAG_ADDR            (uint8_t)0x1E
#define I2C_MAG_READ            (uint8_t)0x3D
#define I2C_MAG_WRITE           (uint8_t)0x3C
#define I2C_MAG_CONF_REGA       (uint8_t)0x00  
#define I2C_MAG_CONF_REGB       (uint8_t)0x01
#define I2C_MAG_MODE_REG        (uint8_t)0x02
#define I2C_MAG_OUT_X_H         (uint8_t)0x03
#define I2C_MAG_OUT_X_L         (uint8_t)0x04
#define I2C_MAG_OUT_Z_H         (uint8_t)0x05
#define I2C_MAG_OUT_Z_L         (uint8_t)0x06
#define I2C_MAG_OUT_Y_H         (uint8_t)0x07
#define I2C_MAG_OUT_Y_L         (uint8_t)0x08
#define I2C_MAG_STATUS_REG      (uint8_t)0x09
#define I2C_MAG_ID_REGA         (uint8_t)0x0A       /* Only read register:'H' */
#define I2C_MAG_ID_REGB         (uint8_t)0x0B       /* Only read register:'4' */
#define I2C_MAG_ID_REGC         (uint8_t)0x0C       /* Only read register:'3' */

#endif  /* _USE_LSM330_BOARD_ */

 /****************************************************************
 * 							GLOBAL
 ****************************************************************/
 

 /****************************************************************
 * 						   FUNCTIONS
 ****************************************************************/
//	Initiate sensor
uint32_t myIMUInit(void);
BOOL myGyrInit(void);
BOOL myAccInit(void);
BOOL myMagInit(void);

// Read data from sensor
BOOL myIMURead(INT_IMU *imu);
BOOL myAccRead(INT_3D  *acc);
BOOL myGyrRead(INT_3D  *gyr);
BOOL myMagRead(INT_3D  *mag);

BOOL myInt2Real(CONVERT_TYPE type, INT_3D *i, REAL_3D *r);
int16_t myTwoComp2Int(uint8_t *MSB, uint8_t *LSB);
void myReal2Bytes(REAL  r, uint8_t * byte);
void myBytes2Real(REAL *r, uint8_t * byte);

void myTestIMU(void);

#endif /* MYIMU_H */



